package hms.employees;

import hms.database.DBRowObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * TerminationReason contains the reason for terminating an employee, and
 * methods to communicate to the database.
 * 
 * @author Ben
 * 
 */
public class TerminationReason extends DBRowObject {
	
	private String terminationReason;
	
	
	/**
	 * This no-arg constructor creates a "Undefined" termination reason.
	 */
	public TerminationReason() {
		super();
		terminationReason = "Undefined";
	}
	
	/**
	 * This constructor is used to look up a individual record from the database.
	 * 
	 * @param uniqueID Vaild unique identifier.
	 */
	public TerminationReason(int uniqueID) {
		super(uniqueID);
	}
	
	/**
	 * Creates a new TerminationReason object from a string. The reason will not
	 * be saved to the database until commitToDatabase is called on it.
	 * 
	 * @param terminationReason
	 */
	public TerminationReason(String terminationReason) {
		this();
		setTerminationReason(terminationReason);
	}
	
	/**
	 * Get the termination reason returned to you as a string.
	 * 
	 * @return String containing termination reason.
	 */
	public String getTerminationReasonString() {
		return terminationReason;
	}

	public String toString() {
		return getTerminationReasonString();
	}
	
	/**
	 * Set the reason for termination. All characters that are not alphanumeric
	 * or spaces will automatically be dropped.
	 * 
	 * You should NOT call this method unless you intend to universally change
	 * this reason wherever it appears.  If you want to change an individual user's
	 * termination reason, you should create a new TerminationReason object.
	 * 
	 * @post Object will be marked as changed
	 * 
	 * @param reason String containing termination reason.
	 */
	public void setTerminationReason(String reason) {
		terminationReason = reduceCharset(reason);
		markAsChanged();
	}
	
	
	// Here are the overrided abstract methods that define the relationship between
	// this object and the DB.


	@Override
	protected void loadPreparedStatement(PreparedStatement stmt) throws SQLException {
		stmt.setString(1, terminationReason);
	}



	@Override
	protected String getUniqueIdentifierFieldName() {
		return "FIREREASON_ID";
	}



	@Override
	protected String getTableName() {
		return "FIREREASON";
	}



	@Override
	protected String[] getFieldNames() {
		String[] out = new String[1];
		out[0] = "FIREREASON";
		return out;
	}


	@Override
	protected boolean allowDuplicateRows() {
		return false;
	}

	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		terminationReason = rs.getString("FIREREASON");
	}
	
}
